package trans.model;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import trans.service.AttachmentDAO;
import trans.support.Constants;


public class AirSa extends Sa{
	
	private java.lang.String concept;
	private java.lang.String origin;
	private java.lang.String destination;
	
	private java.lang.Float estimatedWeight;
	private java.lang.String product;
	private java.util.Date flightDate;
	private java.util.Date estimatedPickupDate;
	private java.util.Date estimatedNeededDateInDestination;
	private java.lang.String hawbPaymentOptions;
	private java.lang.String volumeCutAllowed;
	private java.lang.Integer flyUsaAllowed;
	private java.lang.Integer flyNrtAllowed;
	private java.lang.String hawbNo;
	private java.lang.String mawbNo;
	private java.lang.String flightNo;
	private String dlvPlace;
	private String isAuthorization;
	
	private static final long serialVersionUID = 1L;

	private static final String NO_SPLIT_FLAG = "-";
	
	private String mawbNoFirst;
	private String mawbNoSeconde;
	private String mawbNoThd;
	
	private String hawbNoFirst;
	private String hawbNoSeconde;
	

	public String getIsAuthorization() {
		return isAuthorization;
	}

	public void setIsAuthorization(String isAuthorization) {
		this.isAuthorization = isAuthorization;
	}

	public String getMawbNoThd() {
		return mawbNoThd;
	}

	public void setMawbNoThd(String mawbNoThd) {
		this.mawbNoThd = mawbNoThd;
	}

	public String getMawbNoFirst() {
		return mawbNoFirst;
	}

	public void setMawbNoFirst(String mawbNoFirst) {
		this.mawbNoFirst = mawbNoFirst;
	}

	public String getMawbNoSeconde() {
		return mawbNoSeconde;
	}

	public void setMawbNoSeconde(String mawbNoSeconde) {
		this.mawbNoSeconde = mawbNoSeconde;
	}

	public String getHawbNoFirst() {
		return hawbNoFirst;
	}

	public void setHawbNoFirst(String hawbNoFirst) {
		this.hawbNoFirst = hawbNoFirst;
	}

	public String getHawbNoSeconde() {
		return hawbNoSeconde;
	}

	public void setHawbNoSeconde(String hawbNoSeconde) {
		this.hawbNoSeconde = hawbNoSeconde;
	}
	
	
	public String getDlvPlace() {
		return dlvPlace;
	}

	public void setDlvPlace(String dlvPlace) {
		this.dlvPlace = dlvPlace;
	}

	@Override
	public  boolean isFreeze(User invorkUser){
		if(invorkUser.getUserRole() == Constants.ROLE_ADMIN) return false;
		if(this.getState() >= Constants.AIRSA_STATUS_DEP){
			return true;
		}
		return false;
	}
	
	@Override
	public String getStateShow(){
		return Constants.AIRSA_STATES.get(this.getState());
	}
	
	public void combinAwb(){
		if(StringUtils.isEmpty(mawbNoFirst) == false &&
				StringUtils.isEmpty(mawbNoSeconde) == false){
			this.setMawbNo(this.mawbNoFirst.trim() + NO_SPLIT_FLAG + this.mawbNoSeconde.trim());
		}
		if(StringUtils.isEmpty(hawbNoFirst) == false &&
				StringUtils.isEmpty(hawbNoSeconde) == false ){
			this.setHawbNo(this.hawbNoFirst.trim() + NO_SPLIT_FLAG + this.hawbNoSeconde.trim());
		}
	}
	
	public void splitAwb(){
		if(StringUtils.isEmpty(this.getMawbNo()) == false){
			this.mawbNoFirst = this.getMawbNo().split(NO_SPLIT_FLAG)[0];
		    this.mawbNoSeconde = this.getMawbNo().split(NO_SPLIT_FLAG)[1];
		}
		
		this.hawbNoFirst = "TRX";
		this.hawbNoSeconde = this.getSaNo().split(" ")[1];
	}
	
	public boolean caculatePendding(){
		if(    StringUtils.isEmpty(this.getSaNo())  ||
				this.getState() == null ||
				this.getClientId() == null ||
				StringUtils.isEmpty(this.getOrigin()) ||
				StringUtils.isEmpty(this.getDestination()) ||
				this.getGatwayId() == null ||
				this.getWeight() == null ||
				this.getPcb() == null ||
				this.getEstimatedDimensions() == null ||
				StringUtils.isEmpty(this.getGoodsNatural()) ||
				StringUtils.isEmpty(this.getOrderNo()) ||
				this.getFlightDate() == null ||
				StringUtils.isEmpty(this.getFlightNo()) ||
				this.getEstimatedTimeArrival() == null ||
				StringUtils.isEmpty(this.getConsolidationAllowed()) ||
				StringUtils.isEmpty(this.getHawbPaymentOptions()) ||
				StringUtils.isEmpty(this.getVolumeCutAllowed()) ||
				this.getFlyUsaAllowed() == null ||
				this.getFlyNrtAllowed() == null ||
				StringUtils.isEmpty(this.getHawbNo()) || 
				StringUtils.isEmpty(this.getMawbNo()) || 
				StringUtils.isEmpty(this.getMawbShipper()) || 
				StringUtils.isEmpty(this.getMawbConsignee()) || 
				StringUtils.isEmpty(this.getHawbShipper()) || 
				StringUtils.isEmpty(this.getHawbConsignee()) 
				){
			return true;
		}
		return false;
	}
	
	@Override
	public  void initStates(User user,Map<Integer,String> usableStates) {
		Integer firstIdex = 1000;
		if(this.getId() == null)
			firstIdex = 0;
		if(this.getId() != null && user.getUserRole() == Constants.ROLE_ADMIN)
			firstIdex = 0;
		if(this.getId() != null && user.getUserRole() == Constants.ROLE_SYSUSER)
			firstIdex = this.getState();
		
		if(user.getIsCustomer())
			firstIdex = -1;
		
		for(Integer eachKey : Constants.AIRSA_STATES.keySet()){
			if(eachKey >= firstIdex){
				usableStates.put(eachKey, Constants.AIRSA_STATES.get(eachKey));
			}
		}
	}
	
	public void prepareSaStateShow(HttpServletRequest request){
		int state = this.getState();
		
		List<SaAttachment> attachments = 
			AttachmentDAO.getAllAttachments(this.getId());
		
		if(state >= Constants.AIRSA_STATUS_RBK){
			request.setAttribute("status_rbk", "rbk.gif");
			if(fileContain(attachments,Constants.AIRSA_STATUS_RBK)){
				request.setAttribute("rbk_attached", true);
			}
		}else{
			request.setAttribute("status_rbk", "rbk_gray.gif");
		}
		if(state >= Constants.AIRSA_STATUS_OWW){
			request.setAttribute("status_oww", "oww.gif");
			if(fileContain(attachments,Constants.AIRSA_STATUS_OWW)){
				request.setAttribute("oww_attached", true);
			}
		}else{
			request.setAttribute("status_oww", "oww_gray.gif");
		}
		if(state >= Constants.AIRSA_STATUS_RCF){
			request.setAttribute("status_rcf", "rcf.gif");
			if(fileContain(attachments,Constants.AIRSA_STATUS_RCF)){
				request.setAttribute("rcf_attached", true);
			}
		}else{
			request.setAttribute("status_rcf", "rcf_gray.gif");
		}
		if(state >= Constants.AIRSA_STATUS_RCD){
			request.setAttribute("status_rcd", "rcd.gif");
			if(fileContain(attachments,Constants.AIRSA_STATUS_RCD)){
				request.setAttribute("rcd_attached", true);
			}
		}else{
			request.setAttribute("status_rcd", "rcd_gray.gif");
		}
		if(state >= Constants.AIRSA_STATUS_SAH){
			request.setAttribute("status_sah", "sah.gif");
			if(fileContain(attachments,Constants.AIRSA_STATUS_SAH)){
				request.setAttribute("sah_attached", true);
			}
		}else{
			request.setAttribute("status_sah", "sah_gray.gif");
		}
		if(state >= Constants.AIRSA_STATUS_RFC){
			request.setAttribute("status_rfc", "rfc.gif");
			if(fileContain(attachments,Constants.AIRSA_STATUS_RFC)){
				request.setAttribute("rfc_attached", true);
			}
		}else{
			request.setAttribute("status_rfc", "rfc_gray.gif");
		}
		if(state >= Constants.AIRSA_STATUS_DEP){
			request.setAttribute("status_dep", "dep.gif");
			if(fileContain(attachments,Constants.AIRSA_STATUS_DEP)){
				request.setAttribute("dep_attached", true);
			}
		}else{
			request.setAttribute("status_dep", "dep_gray.gif");
		}
		if(state >= Constants.AIRSA_STATUS_ARR){
			if(fileContain(attachments,Constants.AIRSA_STATUS_ARR)){
				request.setAttribute("arr_attached", true);
			}
			request.setAttribute("status_arr", "arr.gif");
		}else{
			request.setAttribute("status_arr", "arr_gray.gif");
		}
		if(state >= Constants.AIRSA_STATUS_NFD){
			if(fileContain(attachments,Constants.AIRSA_STATUS_NFD)){
				request.setAttribute("nfd_attached", true);
			}
			request.setAttribute("status_nfd", "nfd.gif");
		}else{
			request.setAttribute("status_nfd", "nfd_gray.gif");
		}
		if(state >= Constants.AIRSA_STATUS_DEL){
			if(fileContain(attachments,Constants.AIRSA_STATUS_DEL)){
				request.setAttribute("del_attached", true);
			}
			request.setAttribute("status_del", "del.gif");
		}else{
			request.setAttribute("status_del", "del_gray.gif");
		}
	};
	
	private  boolean fileContain(List<SaAttachment> attachments,Integer state){
		for(SaAttachment each : attachments){
			if(each.getRelativeStatus().equals(state)){
				return true;
			}
		}
		return false;
	}
	
	public void prepareTransNoShow(HttpServletRequest request) {
		String flightDate = "";
		String flightNo = "";
		if(this.getFlightDate() != null){
			 flightDate = DateFormatUtils.format(this.getFlightDate(), "MMM dd,yyyy", Locale.ENGLISH);
		}
		if(StringUtils.isNotEmpty(this.getFlightNo())){
			flightNo = this.getFlightNo();
		}
		
//		"SHIPMENT BOOKED ONTO"
		StringBuilder sb = new StringBuilder("SHIPMENT BOOKED ONTO:"+flightNo +"/"+flightDate);
		for(SaStateHis each : this.getStateHises()){
			if(each!=null && each.getStateTo() == Constants.AIRSA_STATUS_DLV &&
					StringUtils.isNotBlank(each.getRelativeFlightNo()) &&
					each.getCreateTime() != null){
				sb.append("; CONNNECT TO " + each.getRelativeFlightNo() + "/"+ DateFormatUtils.format(each.getCreateTime(), "MMM dd,yyyy", Locale.ENGLISH));
			}
		}
		
		request.setAttribute("depTitle", sb.toString());
	}
	
	@Override
	public String getViewName() {
		return "airSaView";
	}
	
	@Override
	public String getEditName() {
		return "airSaEdit";
	}

	public java.lang.String getConcept() {
		return concept;
	}

	public void setConcept(java.lang.String concept) {
		this.concept = concept;
	}

	public java.lang.String getOrigin() {
		return origin;
	}

	public void setOrigin(java.lang.String origin) {
		this.origin = origin;
	}

	public java.lang.String getDestination() {
		return destination;
	}

	public void setDestination(java.lang.String destination) {
		this.destination = destination;
	}

	public java.lang.Float getEstimatedWeight() {
		return estimatedWeight;
	}

	public void setEstimatedWeight(java.lang.Float estimatedWeight) {
		this.estimatedWeight = estimatedWeight;
	}

	public java.lang.String getProduct() {
		return product;
	}

	public void setProduct(java.lang.String product) {
		this.product = product;
	}

	public java.util.Date getFlightDate() {
		return flightDate;
	}

	public void setFlightDate(java.util.Date flightDate) {
		this.flightDate = flightDate;
	}

	public java.util.Date getEstimatedPickupDate() {
		return estimatedPickupDate;
	}

	public void setEstimatedPickupDate(java.util.Date estimatedPickupDate) {
		this.estimatedPickupDate = estimatedPickupDate;
	}

	public java.util.Date getEstimatedNeededDateInDestination() {
		return estimatedNeededDateInDestination;
	}

	public void setEstimatedNeededDateInDestination(
			java.util.Date estimatedNeededDateInDestination) {
		this.estimatedNeededDateInDestination = estimatedNeededDateInDestination;
	}
	

	public java.lang.String getHawbPaymentOptions() {
		return hawbPaymentOptions;
	}

	public void setHawbPaymentOptions(java.lang.String hawbPaymentOptions) {
		this.hawbPaymentOptions = hawbPaymentOptions;
	}

	public java.lang.String getVolumeCutAllowed() {
		return volumeCutAllowed;
	}

	public void setVolumeCutAllowed(java.lang.String volumeCutAllowed) {
		this.volumeCutAllowed = volumeCutAllowed;
	}

	public java.lang.Integer getFlyUsaAllowed() {
		return flyUsaAllowed;
	}

	public void setFlyUsaAllowed(java.lang.Integer flyUsaAllowed) {
		this.flyUsaAllowed = flyUsaAllowed;
	}

	public java.lang.Integer getFlyNrtAllowed() {
		return flyNrtAllowed;
	}

	public void setFlyNrtAllowed(java.lang.Integer flyNrtAllowed) {
		this.flyNrtAllowed = flyNrtAllowed;
	}


	public java.lang.String getHawbNo() {
		return hawbNo;
	}

	public void setHawbNo(java.lang.String hawbNo) {
		this.hawbNo = hawbNo;
	}

	public java.lang.String getMawbNo() {
		return mawbNo;
	}

	public void setMawbNo(java.lang.String mawbNo) {
		this.mawbNo = mawbNo;
	}

	public java.lang.String getFlightNo() {
		return flightNo;
	}

	public void setFlightNo(java.lang.String flightNo) {
		this.flightNo = flightNo;
	}
	
}
